<template>
  <div style="background-color: white; margin-top: -5px; min-height: 100vh;">
    <van-form @submit="submitForm">
      <searchable-picker
        v-model="form.orderId"
        :columns="orderList"
        valueKey="orderId"
        labelKey="orderName"
        label="项目名称"
        placeholder="请选择项目"
        readonly
      />

      <searchable-picker
        v-model="form.customerId"
        :columns="customerList"
        valueKey="customerId"
        labelKey="customerName"
        label="客户名称"
        placeholder="请选择客户名称"
        readonly
      />

      <van-field
        v-model="form.totalMoney"
        label="合同金额"
        placeholder="请输入合同金额"
        type="number"
        :rules="[{ required: true, message: '请输入合同金额' }]"
      />

      <van-field
        v-model="form.totalTime"
        label="合同时间"
        placeholder="请选择合同时间"
        :rules="[{ required: true, message: '合同时间不能为空' }]"
      />

      <van-field v-model="form.remark" label="备注" placeholder="请输入备注" />

      <searchable-picker
        v-model="form.isContract"
        :columns="dict.type.crm_yes_no"
        valueKey="value"
        labelKey="label"
        label="是否有合同"
        placeholder="请选择是否有合同"
        :rules="[{ required: true, message: '是否有合同不能为空' }]"
      />

      <van-field
        name="uploader"
        label="电子合同"
        v-if="form.isContract === '1'"
      >
        <template #input>
          <file-upload v-model="form.contractUrl" />
        </template>
      </van-field>

      <searchable-picker
        v-model="form.isPurchaseContract"
        :columns="dict.type.crm_yes_no"
        valueKey="value"
        labelKey="label"
        label="有无采购合同"
        placeholder="请选择是否有采购合同"
        :rules="[{ required: true, message: '是否是否有采购合同' }]"
      />

      <van-field
        name="uploader"
        label="采购合同"
        v-if="form.isPurchaseContract === '1'"
      >
        <template #input>
          <file-upload v-model="form.purchaseContractUrl" />
        </template>
      </van-field>

       <div class="mobile-footer">
        <van-button
          type="info"
          native-type="submit"
          style="width: 90%; border-radius: 10px"
          >办理</van-button
        >
      </div>
    </van-form>
     <auditComponent ref="auditComponent" @handle="handleBtn"></auditComponent>
  </div>
</template>
<script>
import { getContract, audit } from "@/api/crm/contract";

import { getCrmOrderAllNoPage } from "@/api/crm/order";
import { getCrmCustomerAllNoPage } from "@/api/crm/customer";
import auditComponent from "../components/auditComponent.vue";

export default {
  name: "Approve",
  components: { auditComponent },
  dicts: ["crm_yes_no"],
  data() {
    return {
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 指定跳转节点
      nodeCode: "",
      customerList: [],
      orderList: [],
      value: '',
      taskId: ''
    };
  },
  created() {
    this.value = this.$route.query.value;
    this.taskId = this.$route.query.taskId;
    this.getCrmCustomerAllNoPage();
    this.getCrmOrderAllNoPage();
    this.getContract();
  },
  methods: {
    getCrmOrderAllNoPage() {
      getCrmOrderAllNoPage({}).then((res) => {
        this.orderList = res.data;
      });
    },
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },
    getCrmCustomerAllNoPage() {
      getCrmCustomerAllNoPage({}).then((res) => {
        this.customerList = res.data;
      });
    },
    async getContract() {
      await getContract(this.value).then((response) => {
        if (!response.data) {
          this.$modal.alertWarning("代办任务不存在");
        }
        this.form = response.data;
        this.title = this.form.createBy + " 项目合同申请";
      });
    },
    submitForm(){
      this.$refs.auditComponent.open();
    },
    /** 审核通过按钮 */
    handleBtn(obj) {
      this.form.taskId = this.taskId;
      this.form.message = obj.message
      this.form.signature = obj.signature
      this.form.skipType = obj.skipType
      audit(this.form).then((response) => {
        this.$toast.success("办理成功");
        this.$router.replace("/flow/todo")
      }); 
    },
  },
};
</script>

